import platform

def get_windows_version():
    version = platform.system()
    if version == "Windows":
        release = platform.release()
        if release == "10":
            return True
        elif release == "11":
            return True
        elif release == "7":
            return True
        elif release == "8" or release == "8.1":
            return True
        else:
            return True
    else:
        return False

if get_windows_version() == False:
    print('你并不是Windows')
    quit()
if __name__ =='__main__':
    try:
        import requests
        from tqdm.rich import trange
    except ModuleNotFoundError:
        os.system('pip3 install --upgrade pip -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com')
        os.system('pip3 install requests tqdm trange rich -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com')
    import os
    all_choise = ['1.5b','7b','8b','14b','32b','70b','671b']
    all_GB = [1.1,4.7,4.9,9,20,43,404]
    choise = None
    def GB_file(directory, size_gb):
        # 将GB转换为字节
        size_bytes = size_gb * 1024 * 1024 * 1024
        
        # 遍历目录中的所有文件
        for filename in os.listdir(directory):
            filepath = os.path.join(directory, filename)
            
            # 确保是文件而不是目录
            if os.path.isfile(filepath):
                # 检查文件大小是否至少为1GB
                if os.path.getsize(filepath) >= size_bytes:
                    return True
        
        # 如果没有找到，返回False
        return False
    def main():
            while 1:
                global all_choise, choise
                choise = input("""请选择多少B：(可支持如下)
    1. 1.5b  1.1GB(推荐)
    2. 7b    4.7GB(推荐)
    3. 8b    4.9GB(推荐)
    4. 14b   9.0GB(推荐)
    5. 32b   20GB(不推荐)
    6. 70b   43GB(不推荐)
    7. 671b  404GB(deepseek满血版)(不推荐)：""")
                if int(choise)+1 > 8 or int(choise)-1 < 0:
                    continue
                else:
                    return int(choise)
    def download():
        url = "https://ollama.com/download/OllamaSetup.exe"
        response = requests.get(url, stream=True)

        # 获取文件大小
        file_size = int(response.headers.get("Content-Length", 0))

        # 设置文件名
        file_name = url.split("/")[-1]

        with open(file_name, "wb") as f:
            with trange(total=file_size, unit="B", unit_scale=True, desc=file_name, ascii=False) as progress_bar:
                for chunk in response.iter_content(chunk_size=1024):
                    if chunk:
                        f.write(chunk)
                        progress_bar.update(len(chunk))
    try:
        if os.path.exists('C:/Users/Administrator/AppData/Local/Programs/Ollama'):
            s = main()
            tea = os.getenv('Ollama_models')
            tea2 = f'{tea}/blobs'
            if GB_file(tea2,all_GB[s - 1]) == True:
                os.system(f'ollama run deepseek-r1:{all_choise[s - 1]}')
            else:
                os.system(f'ollama run deepseek-r1:{all_choise[s - 1]}')
        else:
            os.system(f'start {os.getcwd()}/OllamaSetup.exe')
            pass
        try:
            with open('OllamaSetup.exe','r',encoding='utf-8') as file:
                file.close()
        except FileNotFoundError:
            download()
    except KeyboardInterrupt:
        exit()